package com.xjj.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName MqConfig
 * @Description TODO
 * @Author GH
 * @Date 2022/10/26 16:56
 * @Version 1.0
 **/
@Configuration
public class MqConfig {
    /**
     * @param :
     * @return TopicExchange
     * @author GH
     * @description TODO 声明交换机
     * @date 2022/10/26 17:04
     */
    @Bean
    public TopicExchange voteTimeOutExchange(){
        TopicExchange voteTimeOutExchange = new TopicExchange("voteTimeOutExchange");
        voteTimeOutExchange.setDelayed(true);
        return voteTimeOutExchange;
    }
    /**
     * @param :
     * @return Queue
     * @author GH
     * @description TODO 声明队列
     * @date 2022/10/26 17:06
     */
    @Bean
    public Queue voteTimeOut(){
        Map<String, Object> arguments=new HashMap<>();
        arguments.put("x-message-ttl",1);
        arguments.put("x-dead-letter-exchange","exchangevotedlx");
        arguments.put("x-dead-letter-routing-key","votedlx");
        return new Queue("voteTimeOut",true,false,false,arguments);
        //return new Queue("voteTimeOut");
    }

    @Bean
    public Binding bingVoteTimeOut(){
        return BindingBuilder.bind(voteTimeOut()).to(voteTimeOutExchange()).with("voteTimeOut");
    }

    //定义死信交换机和死信队列
    @Bean
    public TopicExchange exchangevotedlx(){
        return new TopicExchange("exchangevotedlx");
    }

    //声明队列
    @Bean
    public Queue queuevotedlx(){
        return new Queue("queuevotedlx");
    }

    @Bean
    public Binding bindingvotedlx(){
        return BindingBuilder.bind(queuevotedlx()).to(exchangevotedlx()).with("#.queuevotedlx.#");
    }


    /**
     * @param :
     * @return MessageConverter
     * @author GH
     * @description TODO 使用jason字符串解析对象
     * @date 2022/10/26 17:39
     */
    @Bean
    public MessageConverter messageConverter(){
        return new Jackson2JsonMessageConverter();
    }


}
